
#include <BFont>
#include <BStyle>
#include "member_BIcon.h"

using namespace BWE;

member_BIcon::member_BIcon(BIcon* icon)
{
	boss = icon;
}
member_BIcon::~member_BIcon()
{

}

void member_BIcon::generateImageForState(State state)
{
	BImage* image = images[State_Normal];
	if (!image)
		return;
	BImage* state_image = 0;
	switch (image->format())
	{
	case Format_RGBA:
		state_image = convertRGBA(image, state);
		break;
	case Format_RGB:
		state_image = convertRGB(image, state);
		break;
	case Format_Gray:
		state_image = convertGray(image, state);
		break;
	case Format_Gray_Alpha:
		state_image = convertGrayAlpha(image, state);
		break;
	default:
		break;
	}
	images[state] = state_image;
}

BImage* member_BIcon::convertRGBA(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BColor* data0 = (BColor*)newimage->pixels()->data();
	BColor* data1 = (BColor*)image->pixels()->data();
	int count = image->size().width() * image->size().height();
	for (int i = 0; i < count; i++)
	{
		data0[i] = data1[i];
		BColor& color = data0[i];
		switch (state)
		{
		case State_Pressed:
		{
			color.r() *= 0.83f;
			color.g() *= 0.83f;
			color.b() *= 0.83f;
			break;
		}
		case State_Hovered:
		{
			int r = color.r() * 1.16f;
			int g = color.g() * 1.16f;
			int b = color.b() * 1.16f;
			color.r() = r > 255 ? 255 : r;
			color.g() = g > 255 ? 255 : g;
			color.b() = b > 255 ? 255 : b;
			break;
		}
		case State_Checked:
		{
			int r = color.r() * 1.24f;
			int g = color.g() * 1.24f;
			int b = color.b() * 1.24f;
			color.r() = r > 255 ? 255 : r;
			color.g() = g > 255 ? 255 : g;
			color.b() = b > 255 ? 255 : b;
			break;
		}
		case State_Disable:
		{
			BByte gray = color.gray();
			color.r() = gray;
			color.g() = gray;
			color.b() = gray;
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* member_BIcon::convertRGB(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height() * 3;
	for (int i = 0; i < count; i += 3)
	{
		BByte& r = data0[i];
		BByte& g = data0[i + 1];
		BByte& b = data0[i + 2];
		r = data1[i];
		g = data1[i + 1];
		b = data1[i + 2];
		switch (state)
		{
		case State_Pressed:
		{
			r *= 0.83f;
			g *= 0.83f;
			b *= 0.83f;
			break;
		}
		case State_Hovered:
		{
			int cr = r * 1.16f;
			int cg = g * 1.16f;
			int cb = b * 1.16f;
			r = cr > 255 ? 255 : cr;
			g = cg > 255 ? 255 : cg;
			b = cb > 255 ? 255 : cb;
			break;
		}
		case State_Checked:
		{
			int cr = r * 1.24f;
			int cg = g * 1.24f;
			int cb = b * 1.24f;
			r = cr > 255 ? 255 : cr;
			g = cg > 255 ? 255 : cg;
			b = cb > 255 ? 255 : cb;
			break;
		}
		case State_Disable:
		{
			BByte gray = (r + g + b) / 3;
			r = gray;
			g = gray;
			b = gray;
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* member_BIcon::convertGray(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height();
	for (int i = 0; i < count; i++)
	{
		data0[i] = data1[i];
		switch (state)
		{
		case State_Pressed:
		{
			data0[i] *= 1.24f;
			break;
		}
		case State_Hovered:
		{
			int gray = data0[i] * 1.64f;
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Checked:
		{
			int gray = data0[i] * 1.36f;
			data0[i] = gray > 255 ? 255 : gray;
			break;
		}
		case State_Disable:
		{
			data0[i] *= 0.83f;
			break;
		}
		default:
			break;
		}
	}
	return newimage;
}
BImage* member_BIcon::convertGrayAlpha(BImage* image, int state)
{
	BImage* newimage = new BImage(image->size(), image->format());
	BByte* data0 = newimage->pixels()->data();
	BByte* data1 = image->pixels()->data();
	int count = image->size().width() * image->size().height() * 2;
	for (int i = 0; i < count; i += 2)
	{
		data0[i] = data1[i];
		data0[i + 1] = data1[i + 1];
		if (state == State_Pressed)
		{
			data0[i] *= 0.24f;
		}
		else if (state == State_Hovered)
		{
			int gray = data0[i] * 1.64f;
			data0[i] = gray > 255 ? 255 : gray;
		}
		else if (state == State_Disable)
		{

		}
	}
	return newimage;
}
